package com.strategy.demo.config;

import com.strategy.demo.Service.PaymentService;
import com.strategy.demo.model.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Chill_Lyn
 * @Date 2021/4/30 11:24
 */
@Component
public class PaymentStrategy {

    @Resource
    private List<PaymentService> paymentServices;

    /**
     * 支付
     * @param order
     * @return
     */
    public void pay(Order order) {
        PaymentService paymentService = paymentServices.stream()
                .filter(service -> service.isSupport(order))
                .findFirst()
                .orElseThrow(()-> new IllegalArgumentException("pay type not supported."));

        paymentService.pay(order);
    }
}
